// goods/pages/goodsDetails/goodsDetails.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let login = require('../../../common/script/login');
let wxParse = require('../../wxParse/wxParse.js');
const utils = require('../../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: '',
    amountVal: 1
  },
  getData(id) {
    const that = this;
    let data = {
      id: that.data.goodsId,
      token: wx.getStorageSync('token'),
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.goodsInfo, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        that.setData({
          centerData: res.data.result,
        })
        let article = res.data.result.goods_content;
        wxParse.wxParse('article', 'html', article, that, 0);
      }
    })
  },
  //输入
  bindKeyInput(e) {
    const that = this;
    that.setData({
      amountVal: e.detail.value
    })
  },
  // 点击加号
  add() {
    const that = this;
    let amountVal = that.data.amountVal;
    amountVal++;
    that.setData({
      amountVal
    })

  },
  // 点击减号
  minus() {
    const that = this;
    let amountVal = that.data.amountVal;
    if (amountVal <= 1) {
      that.setData({
        amountVal
      })
    } else {
      amountVal--;
      that.setData({
        amountVal
      })
    }
  },
  // 去下单
  toSubmitOrder() {
    const that = this;
    wx.navigateTo({
      url: `/goods/pages/submitOrder/submitOrder?goodsId=${that.data.goodsId}&goodsNum=${that.data.amountVal}&storeId=${that.data.centerData.store.store_id}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.setData({
      goodsId: options.id
    })
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})